package hui.com.service.impl;

import hui.com.dao.SalesForecastDao;
import hui.com.model.req.sales.RequestGetLikeSalesForecast;
import hui.com.model.req.sales.RequestGetSalesForecast;
import hui.com.model.req.sales.RequestGetSalesForecastHistory;
import hui.com.model.res.sales.ResponeGetSalesForecast;
import hui.com.model.res.sales.ResponseGetSalesForecastList;
import hui.com.util.DateUtil;
import hui.com.model.entity.sales.SalesForecastEntity;
import hui.com.service.SalesForecastService;
import hui.com.util.IntegerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

@Service
public class SalesForecastServiceImpl implements SalesForecastService {

    @Resource
    @Autowired
    SalesForecastDao salesForecastDao;
    @Override
    public Boolean addSaleForecast(SalesForecastEntity sales) {
        return salesForecastDao.addSaleForecast(sales);
    }

    @Override
    public SalesForecastEntity getSalesForecast(SalesForecastEntity sales) {
        return salesForecastDao.getSalesForecast(sales);
    }

    @Override
    public ResponeGetSalesForecast getPrevSalesForecast(RequestGetSalesForecast sales) {
        return salesForecastDao.getPrevSalesForecast(sales);
    }

    @Override
    public ResponeGetSalesForecast getNextSalesForecast(RequestGetSalesForecast sales) {
        return salesForecastDao.getNextSalesForecast(sales);
    }

    @Override
    public ResponseGetSalesForecastList getListSalesForecast(RequestGetSalesForecast sales) {
        ResponseGetSalesForecastList salesRespone = new ResponseGetSalesForecastList();
        int currentPage = sales.getCurrentPage()!=null?sales.getCurrentPage():0;

        int pageSize =( sales.getPageSize()!=null && sales.getPageSize()!=0)?sales.getPageSize():30;
        Integer currentIndex = currentPage-1>0?((currentPage-1)*pageSize):pageSize;

        sales.setCurrentIndex(currentIndex);
        sales.setPageSize(pageSize);
        salesRespone.setSalesList(salesForecastDao.getListSalesForecast(sales));
        salesRespone.setPageSizeMax(salesForecastDao.getListSalesForecastPage(sales));

        return salesRespone;
    }

    @Override
    public Integer getListSalesForecastPage(RequestGetSalesForecast sales) {
        return salesForecastDao.getListSalesForecastPage(sales);
    }

    @Override
    public ResponseGetSalesForecastList getLikeListSalesForecast(RequestGetLikeSalesForecast sales) {
//        out.println(sales);
        ResponseGetSalesForecastList salesRespone = new ResponseGetSalesForecastList();
        int currentPage = sales.getCurrentPage()!=null?sales.getCurrentPage():0;

        int pageSize =( sales.getPageSize()!=null && sales.getPageSize()!=0)?sales.getPageSize():30;
        Integer currentIndex = currentPage-1>=0?((currentPage-1)*pageSize):pageSize;

        sales.setCurrentIndex(currentIndex);
        sales.setPageSize(pageSize);
        salesRespone.setSalesList(salesForecastDao.getLikeListSalesForecast(sales));
        salesRespone.setPageSizeMax(salesForecastDao.getLikeListSalesForecastPage(sales));
        return salesRespone;
    }

    @Override
    public Integer getLikeListSalesForecastPage(RequestGetLikeSalesForecast sales) {
        return salesForecastDao.getLikeListSalesForecastPage(sales);
    }

    @Override
    public ResponeGetSalesForecast getSalesForecast_User(SalesForecastEntity sales) {
        return salesForecastDao.getSalesForecast_User(sales);
    }


    @Override
    public Integer updateSalesForecast(SalesForecastEntity sales) {
        return salesForecastDao.updateSalesForecast(sales);
    }

    @Override
    public Boolean delSalesForecast(int id) {
        return salesForecastDao.delSalesForecast(id);
    }

    @Override
    public ResponeGetSalesForecast getHistorySales(RequestGetSalesForecast sales) {
        return salesForecastDao.getHistorySalesForecast(sales);
    }

    @Override
    public List<ResponeGetSalesForecast> getHistoryListSalesForecast(RequestGetSalesForecast sales){
        return salesForecastDao.getHistoryListSalesForecast(sales);
    }

    @Override
    public Integer getHistoryListSalesForecastPage(RequestGetSalesForecast sales) {
        return salesForecastDao.getHistoryListSalesForecastPage(sales);
    }


    @Override
    public Boolean addHistorySaleForecast(SalesForecastEntity sales) {
        return salesForecastDao.addHistorySaleForecast(sales);
    }

    @Override
    public Boolean addCustName(String name) {
        return salesForecastDao.addCustName(name);
    }

    @Override
    public String[] getCustNameArray(String name) {
        return salesForecastDao.getCustNameArray(name);
    }

    @Override
    public ResponseGetSalesForecastList getSalesForecastHistoryList_User(RequestGetSalesForecastHistory sales) {
        ResponseGetSalesForecastList responseGetSalesForecastList = new ResponseGetSalesForecastList();
        responseGetSalesForecastList.setSalesList(salesForecastDao.getSalesForecastHistoryList_User(sales));

        return responseGetSalesForecastList;
    }

    @Override
    public ResponeGetSalesForecast getSalesForecastHistory_User(RequestGetSalesForecastHistory sales) {
        return salesForecastDao.getSalesForecastHistory_User(sales);
    }

    @Override
    public Boolean getCustNameExist(String name) {
        return salesForecastDao.getCustNameExist(name);
    }

    @Override
    public String salesForecastADD_getSalesCode(){
        RequestGetSalesForecast sales = new RequestGetSalesForecast();
        RequestGetSalesForecast saleCode = new RequestGetSalesForecast();

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前月第一天的最小时间
        LocalDateTime firstDayOfMonth =  LocalDateTime.of(currentDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN);
        // 获取当前月的最大时间（年月日秒）
        LocalDateTime lastDayOfMonth = LocalDateTime.of(currentDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX);
        sales.setStrTime(firstDayOfMonth);
        sales.setEndTime(lastDayOfMonth);
        Integer size =  salesForecastDao.getListSalesForecastPage(sales);

        size = (size==null || size==0)?1:size;
//        out.println(size);
        String salesCode = null;

        do{

            String salesCodes = IntegerUtil.getBH(size,4);

            salesCodes ="SALES-F-" + DateUtil.currentYearMontID() + "-" + salesCodes;

            saleCode.setSalesForecastCode(salesCodes);

            salesCode = salesCodes;

            size++;

//            out.println(salesCodes);
//            out.println(salesDao.getListSalesPage(salesEntityCode,null,null));
//            out.println(salesDao.getListSalesPage(salesEntityCode,null,null)<=0);
        }while (salesForecastDao.getListSalesForecastPage(saleCode)>0);


        return salesCode;
    }
}
